/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 多页夹视图
    /// </summary>
    public class FCTabPage : FCDiv {
        /// <summary>
        /// 创建多页夹
        /// </summary>
        public FCTabPage() {
        }

        public FCButton m_headerButton;

        /// <summary>
        /// 获取页头按钮
        /// </summary>
        public virtual FCButton getHeaderButton()
        {
            return m_headerButton;
        }

        /// <summary>
        /// 设置页头按钮
        /// </summary>
        public virtual void setHeaderButton(FCButton value)
        {
            m_headerButton = value;
        }

        public FCPoint m_headerLocation;

        /// <summary>
        /// 获取头部的位置
        /// </summary>
        public virtual FCPoint getHeaderLocation()
        {
            return m_headerLocation;
        }

        /// <summary>
        /// 设置头部的位置
        /// </summary>
        public virtual void setHeaderLocation(FCPoint value)
        {
            m_headerLocation = value;
        }

        /// <summary>
        /// 获取头部是否可见
        /// </summary>
        public virtual bool isHeaderVisible()
        {
            if (m_headerButton != null)
            {
                return m_headerButton.isVisible();
            }
            else
            {
                return false;
            }
        }

       /// <summary>
        /// 设置头部是否可见
       /// </summary>
        public virtual void setHeaderVisible(bool value)
        {
            if (m_headerButton != null)
            {
                m_headerButton.setVisible(value);
            }
        }

        public FCTabView m_tabView;

        /// <summary>
        /// 获取多页夹视图
        /// </summary>
        public virtual FCTabView getTabView()
        {
            return m_tabView;
        }

        /// <summary>
        /// 设置多页夹视图
        /// </summary>
        public virtual void setTabView(FCTabView value)
        {
            m_tabView = value;
        }

        /// <summary>
        /// 调用事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="invoke"></param>
        public override void callEvent(String eventName, Object sender, Object invoke) {
            base.callEvent(eventName, sender, invoke);
            if (eventName == "ondragbegin"){
                getTabView().onDragTabHeaderBegin(this);
            } else if (eventName == "ondragend") {
                getTabView().onDragTabHeaderEnd(this);
            } else if (eventName == "ondragging") {
                getTabView().onDraggingTabHeader(this);
            }
        }

        /// <summary>
        /// 触摸事件回调
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="touchInfo"></param>
        /// <param name="invoke"></param>
        public override void callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke) {
            base.callTouchEvent(eventName, sender, touchInfo, invoke);
            if (sender == m_headerButton) {
                if (eventName == "ontouchdown") {
                    if (m_tabView != null) {
                        m_tabView.setSelectedTabPage(this);
                        m_tabView.update();
                        m_tabView.invalidate();
                    }
                }
            }
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void delete() {
            if (!isDeleted()) {
                if (m_headerButton != null) {
                    m_headerButton.removeEvent(this, "ondragbegin");
                }
                if (m_headerButton != null) {
                    m_headerButton.removeEvent(this, "ondragend");
                }
                if (m_headerButton != null) {
                    m_headerButton.removeEvent(this, "ondragging");
                }
                if (m_headerButton != null) {
                    m_headerButton.removeEvent(this, "ontouchdown");
                }
                m_headerButton = null;
            }
            base.delete();
        }

        /// <summary>
        /// 开始拖动页头按钮方法
        /// </summary>
        /// <param name="sender">发送者</param>
        public void dragHeaderBegin(Object sender, Object invoke) {
            if (m_tabView != null) {
                m_tabView.onDragTabHeaderBegin(this);
            }
        }

        /// <summary>
        /// 结束拖动页头按钮方法
        /// </summary>
        /// <param name="sender">发送者</param>
        public void dragHeaderEnd(Object sender, Object invoke) {
            if (m_tabView != null) {
                m_tabView.onDragTabHeaderEnd(this);
            }
        }

        /// <summary>
        /// 页头按钮拖动中方法
        /// </summary>
        /// <param name="sender">发送者</param>
        public void draggingHeader(Object sender, Object invoke) {
            if (m_tabView != null) {
                m_tabView.onDraggingTabHeader(this);
            }
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "TabPage";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            if (name == "headersize") {
                type = "size";
                if (m_headerButton != null) {
                    value = FCTran.sizeToStr(m_headerButton.getSize());
                }
                else {
                    value = "0,0";
                }
            }
            else if (name == "headervisible") {
                type = "bool";
                value = FCTran.boolToStr(isHeaderVisible());
            }
            else if (name.IndexOf("header-") != -1) {
                if (m_headerButton != null) {
                    m_headerButton.getAttribute(name.Substring(7), ref value, ref type);
                }
            }
            else {
                base.getAttribute(name, ref value, ref type);
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "Header", "HeaderSize", "HeaderVisible" });
            return attributeNames;
        }

        /// <summary>
        /// 页头触摸按下方法
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="touchInfo">触摸信息</param>
        public void headerTouchDown(Object sender, FCTouchInfo touchInfo, Object invoke) {
            if (m_tabView != null) {
                m_tabView.setSelectedTabPage(this);
                update();
                m_tabView.invalidate();
            }
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            base.onLoad();
            if (m_tabView != null) {
                if (m_headerButton == null) {
                    FCHost host = getNative().getHost();
                    m_headerButton = host.createInternalView(this, "headerbutton") as FCButton;
                    m_headerButton.addEvent(this, "ondragbegin", null);
                    m_headerButton.addEvent(this, "ondragend", null);
                    m_headerButton.addEvent(this, "ondragging", null);
                    m_headerButton.addEvent(this, "ontouchdown", null);
                }
                if (!m_tabView.containsView(m_headerButton)) {
                    m_tabView.addView(m_headerButton);
                }

            }
        }

        /// <summary>
        /// 文字改变方法
        /// </summary>
        public override void onTextChanged() {
            base.onTextChanged();
            if (m_headerButton != null) {
                m_headerButton.setText(getText());
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value) {
            if (name == "headersize") {
                if (m_headerButton != null) {
                    m_headerButton.setAttribute("size", value);
                }
            }
            else if (name == "headervisible") {
                setHeaderVisible(FCTran.strToBool(value));
            }
            else if (name.IndexOf("header-") != -1) {
                if (m_headerButton != null) {
                    m_headerButton.setAttribute(name.Substring(7), value);
                }
            }
            else {
                base.setAttribute(name, value);
            }
        }
    }
}
